﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace ConrtolLibrary
{
    public partial class ValueBar : UserControl
    {
        #region Private Variables 

        int leftPadding = 20;
        int rightPadding = 5;
        int topPadding = 5;
        int bottomPadding = 5;

        int indicatorSize = 15;
        float lineWidth = 2.0f;

        Rectangle BarRect;

        private int _maxVal = 100;
        private int _minVal= -100;
        private int _value = 0;

        private Color _lowColor =  Color.LightGreen;
        private Color _highColor = Color.IndianRed;
        private Color _indicatorColor = Color.Red;

        #endregion

        #region Properties

        public float LineWidth
        {
            get { return lineWidth; }
            set { lineWidth = value; Invalidate(); }
        }

        public int IndicatorSize
        {
            get { return indicatorSize; }
            set { indicatorSize = value; Invalidate(); }
        }

        public int MaxValue
        {
            get
            {
                return _maxVal;
            }
            set
            {
                _maxVal = Math.Max(value, _minVal);
                Invalidate();
            }
        }

        public int MinValue
        {
            get
            {
                return _minVal;
            }
            set
            {
                _minVal = Math.Min(value, _maxVal);
                Invalidate();
            }
        }

        public int Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = Math.Max(Math.Min(value, MaxValue), MinValue);
                Invalidate();
            }
        }

        public Color LowColor
        {
            get
            {
                return _lowColor;
            }
            set
            {
                _lowColor = value;
                Invalidate();
            }
        }

        public Color HighColor
        {
            get
            {
                return _highColor;
            }
            set
            {
                _highColor = value;
                Invalidate();
            }
        }

        public Color IndicatorColor
        {
            get { return _indicatorColor; }
            set { _indicatorColor = value; Invalidate(); }
        }

        #endregion

        public ValueBar()
        {
            InitializeComponent();

            BarRect = new Rectangle(leftPadding, topPadding,
                                   Width - leftPadding - rightPadding,
                                   Height - topPadding - bottomPadding);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Pen pen = new Pen(ForeColor, lineWidth); 


            //recalc drawing area
            BarRect = new Rectangle(leftPadding, topPadding,
                                   Width - leftPadding - rightPadding,
                                   Height - topPadding - bottomPadding);


            //draw main guide lines
            //e.Graphics.DrawLine(pen, new Point(leftPadding - 7, topPadding), new Point(leftPadding, topPadding));
            //e.Graphics.DrawLine(pen, new Point(leftPadding - 7, Height / 2), new Point(leftPadding, Height / 2));
            //e.Graphics.DrawLine(pen, new Point(leftPadding - 7, Height - bottomPadding), new Point(leftPadding, Height - bottomPadding));

            DrawStatus(e.Graphics);

            //draw border
            e.Graphics.DrawRectangle(pen, BarRect);
            //e.Graphics.DrawLine(pen, new Point(leftPadding, Height / 2), new Point(Width - rightPadding, Height / 2));
        }



        private void DrawStatus(Graphics p)
        {
          
            int level;
            int midVal = (MaxValue + MinValue) / 2;

            if (MaxValue == MinValue || Value == midVal) { level = 0; }
            else { level = (int)(Value * ((double)(Height / 2) / (double)(MaxValue - midVal))); }

            Rectangle r;
            LinearGradientBrush radBrush;
            Point[] pts = new Point[3];

            if (level >= 0)
            {
                r = new Rectangle(0, (Height / 2) - level, Width, level);
                radBrush = new LinearGradientBrush(new Point(0, Height / 2), new Point(0, 0), _lowColor, _highColor );
            }
            else
            {
                r = new Rectangle(0, (Height / 2), Width, -level);
                radBrush = new LinearGradientBrush(new Point(0, Height / 2), new Point(0, Height), _lowColor, _highColor);
            }

            //Draw Fill Area
            p.SetClip(BarRect);
            p.FillRectangle(radBrush, r);
            p.ResetClip();

            //Draw Indicator
            using (Pen pen = new Pen(_indicatorColor, lineWidth))
            {
                pts[0] = new Point(leftPadding, (Height / 2) - level);
                pts[1] = new Point(pts[0].X - indicatorSize, pts[0].Y + (int)(indicatorSize * 0.5));
                pts[2] = new Point(pts[0].X - indicatorSize, pts[0].Y - (int)(indicatorSize * 0.5));
                p.FillPolygon(new SolidBrush(_indicatorColor), pts);
                //p.DrawLine(pen, pts[0], pts[1]);
                //p.DrawLine(pen, pts[1], pts[2]);
                //p.DrawLine(pen, pts[0], pts[2]);
                p.DrawLine(pen, pts[0], new Point(Width - rightPadding, pts[0].Y));
                
            }

        }
    }
}
